﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SOM.Helper
{

  public static partial class Methods
  {
    public static void SendToDatabase<T>(object obj, string server,string table,string SchemeFilePath = "")
    {
      System.Reflection.Assembly ass;
      try
      {
        string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Database.Connector.dll");
        
        if (System.IO.File.Exists(path))
        {
          AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = true.ToString();
          ass = System.Reflection.Assembly.LoadFrom(path);
          
          SOM.Interfaces.Helper.IDatabaseMethods db = (SOM.Interfaces.Helper.IDatabaseMethods)ass.CreateInstance("SOM.Database.Connector", true);
          db.SendToDatabase<T>(obj, server,table);
        }
        else
        {
          System.Windows.Forms.MessageBox.Show("File not found! " + (char)34 + "Database.Connector.dll" + (char)34);
        }
      }
      catch (Exception ex)
      {
        System.Windows.Forms.MessageBox.Show(ex.Message);
      }
      finally
      {
        ass = null;
        AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = false.ToString();
      }
    }
    public static object GetFromDatabase<T>(string server, int ID, string table, string SchemeFilePath = "")
    {
      System.Reflection.Assembly ass;
      try
      {
        string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Database.Connector.dll");
        
        if (System.IO.File.Exists(path))
        {
          AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = true.ToString();// = true;
          ass = System.Reflection.Assembly.LoadFrom(path);
          SOM.Interfaces.Helper.IDatabaseMethods db = (SOM.Interfaces.Helper.IDatabaseMethods)ass.CreateInstance("SOM.Database.Connector",true);
          return db.GetFromDatabase<T>(server, ID,table);
        }
        else
        {
          System.Windows.Forms.MessageBox.Show("File not found! " + (char)34 + "Database.Connector.dll" + (char)34);
          return null;
        }
      }
      catch (Exception ex)
      {
        System.Windows.Forms.MessageBox.Show(ex.Message);
        return null;
      }
      finally
      {
        ass = null;
        AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = false.ToString();
      }
    }


    public static double Winkel(SOM.Helper.Types.Point3d p1, SOM.Helper.Types.Point3d p2)
    {
      const double DEG = 180 / Math.PI;

      double angle1 = 0;

      double xLength;
      double yLength;

      xLength = p1.X - p2.X;
      yLength = p1.Y - p2.Y;

      if (xLength == 0 & yLength == 0)
      {
        throw new Exception("Punkte sind gleich!");
      }
      if (Math.Abs(yLength) < 0.11 & xLength < 0)
      {
        angle1 = 0;
        return angle1;
      }
      if (Math.Abs(xLength) < 0.11 & yLength < 0)
      {
        angle1 = 90;
        return angle1;
      }
      if (Math.Abs(yLength) < 0.11 & xLength > 0)
      {
        angle1 = 180;
        return angle1;
      }
      if (Math.Abs(xLength) < 0.11 & yLength > 0)
      {
        angle1 = 270;
        return angle1;
      }


      angle1 = Math.Atan(yLength / xLength);
      angle1 = angle1 * DEG;

      if (p1.X < p2.X & p1.Y < p2.Y)
      {
        angle1 += 0;
      }
      else if (p1.X > p2.X & p1.Y < p2.Y)
      {
        angle1 = Math.Abs(angle1) + 90;
      }
      else if (p1.X > p2.X & p1.Y > p2.Y)
      {
        angle1 += 180;
      }
      else if (p1.X < p2.X & p1.Y > p2.Y)
      {
        angle1 = Math.Abs(angle1) + 270;
      }

      return angle1;

    }
    public static SOM.Helper.Types.Point3d GetPointPolar(SOM.Helper.Types.Point3d Origin, double Angle, double Length, bool InvertLength, int Digits)
    {
      if (InvertLength)
        Length *= -1;
      const double BOG = Math.PI / 180;
      double tmp = Math.Round(Math.Cos(Angle * BOG) * Length, Digits, MidpointRounding.AwayFromZero);
      return CreatePoint3d(Math.Round(Origin.X + tmp, Digits, MidpointRounding.AwayFromZero),
        Math.Round(Origin.Y + Math.Sin(Angle * BOG) * Length, Digits, MidpointRounding.AwayFromZero),
        Origin.Z
      );
    }

    internal static SOM.Helper.Types.Point3d CreatePoint3d(double x, double y, double z)
    {
      SOM.Helper.Types.Point3d p = new SOM.Helper.Types.Point3d();
      p.X = x;
      p.Y = y;
      p.Z = z;
      return p;
    }

    public static string ArrayToString(Array obj)
    {
      if (obj.Length > 0)
      {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("{ ");
        bool first = true;
        string zus = string.Empty;
        if (obj is string[])
        {
          zus = "'";
        }
        foreach (object o in obj)
        {
          if (first)
          {
            sb.Append(zus + o.ToString() + zus);
            first = false;
          }
          else
          {
            sb.Append(" | " + zus + o.ToString() + zus);
          }
        }
        sb.Append(" }");
        return sb.ToString();
      }
      else
      {
        return "{ }";
      }
    }

    internal static double GetDistancePoint2D(SOM.Helper.Types.Point3d p1, SOM.Helper.Types.Point3d p2)
    {
      return Math.Sqrt((Math.Pow(Math.Abs(p2.X - p1.X), 2)) + (Math.Pow(Math.Abs(p2.Y - p1.Y), 2)));
    }
  }

}
